Imports System.Data.SqlClient
Imports DevComponents.DotNetBar
Public Class frmCrea_Consecuencias
    Private mintConsecuencia As Integer
    Public WriteOnly Property CONSECUENCIA() As Integer

        Set(ByVal value As Integer)
            mintConsecuencia = value
        End Set
    End Property
    Private SEPARADOR As Char
    Dim FECHA_ACTUAL As String
    Dim USERID As Integer
    Dim IDACUMULADO As Integer
    Private Sub frmCrea_Consecuencias_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        Me.BackColor = Color.White
        SEPARADOR = Application.CurrentCulture.NumberFormat.NumberDecimalSeparator

        Line3.ForeColor = System.Drawing.Color.FromArgb(224, 224, 224)

        lblClave.ForeColor = System.Drawing.Color.FromArgb(64, 64, 64)
        lblNombre.ForeColor = System.Drawing.Color.FromArgb(64, 64, 64)
        lblMotivo.ForeColor = System.Drawing.Color.FromArgb(64, 64, 64)
        lblMonto.ForeColor = System.Drawing.Color.FromArgb(64, 64, 64)
        lblSolicitante.ForeColor = System.Drawing.Color.FromArgb(64, 64, 64)
        lblAutoriza.ForeColor = System.Drawing.Color.FromArgb(64, 64, 64)


        If PARAMETROS.ENTRADA_dATOS = 1 Then
            txtNombre.CharacterCasing = CharacterCasing.Upper
        ElseIf PARAMETROS.ENTRADA_dATOS = 2 Then
            txtNombre.CharacterCasing = CharacterCasing.Lower
        ElseIf PARAMETROS.ENTRADA_dATOS = 3 Then
            'FORMAT_CULTURA
        ElseIf PARAMETROS.ENTRADA_dATOS = 0 Then
            txtNombre.CharacterCasing = CharacterCasing.Normal
        End If

        '***********************************************************
        '/////////////// COMIENZA CARGA DE LISTAS



        CargaEmpleados()


        Actualizar(mintConsecuencia)

        FECHA_ACTUAL = Now.ToString("MM/dd/yyyy")
        USERID = BuscarIntDatoCitra("SELECT USERID FROM USUARIOS_CHECADOR WHERE BADGENUMBER=" & CInt(txtCodigo.Text))
        IDACUMULADO = BuscarIntDatoCitra("SELECT ASISTE FROM ACUMULADOS WHERE USERID=" & USERID & " AND CONVERT(VARCHAR,FECHA,101)='" & Fecha() & "'")

    End Sub
    Sub GenerarNotificacion(ByVal Texto As String, ByVal Duracion As Integer, ByVal colores As eToastGlowColor)
        Try
            Dim glow As eToastGlowColor = colores
            Dim Poe As eToastPosition
            If Me.WindowState = FormWindowState.Maximized Then
                Poe = eToastPosition.MiddleCenter
            Else
                Poe = eToastPosition.BottomCenter
            End If

            ToastNotification.Show(Me, Texto, Nothing, Duracion * 1000, glow, Poe)
        Catch ex As Exception
            If colores = eToastGlowColor.Red Then
                Citra_MensajeSistema(Texto, RequierenAntencionTipos.OcurrioError)
            Else
                Citra_MensajeSistema(Texto, RequierenAntencionTipos.MuestraStatus)
            End If
        End Try



    End Sub
    Sub Actualizar(ByVal ID As Integer)
        If ID = -1 Then
            Dim IDEMPLEADO As Integer = BuscarIntDatoCitra("SELECT IDEMPLEADO FROM EMPLEADOS WHERE CLAVE='" & txtCodigo.Text & "' AND ESTATUS<>'B'")
            txtNombre.Text = BuscarStrDatoCitra("SELECT NOMBRE_COMPLETO FROM EMPLEADOS WHERE IDEMPLEADO=" & IDEMPLEADO)
            txtAutoriza.Text = BuscarStrDatoCitra("SELECT NOMBRE_RESP FROM DEPARTAMENTOS D INNER JOIN EMPLEADOS E ON D.CLAVE=E.DEPTO WHERE E.IDEMPLEADO=" & IDEMPLEADO)
            txtMotivo.Focus()
            Exit Sub


        End If
        Dim strSql As String = ""
        strSql = "SELECT * FROM CONSECUENCIAS WHERE IDCONSECUENCIA=" & ID

        Dim cnConn As New SqlConnection
        cnConn.ConnectionString = CitraConnection
        Dim cmdBuscar As New SqlCommand
        cmdBuscar.Connection = cnConn
        cmdBuscar.CommandText = strSql
        Dim rdBuscar As SqlDataReader
        cnConn.Open()
        Try
            rdBuscar = cmdBuscar.ExecuteReader

        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Exclamation, "Aviso")
        End Try

        If rdBuscar.Read() Then
            txtCodigo.Text = rdBuscar("CLAVE")
            txtNombre.Text = BuscarStrDatoCitra("SELECT NOMBRE_COMPLETO FROM EMPLEADOS WHERE CLAVE='" & rdBuscar("CLAVE") & "'")
            txtMotivo.Text = rdBuscar("MOTIVO")
            txtMonto.Text = FormatCurrency(rdBuscar("MONTO"))
            txtSolicita.Text = rdBuscar("LEVANTO")
            txtAutoriza.Text = rdBuscar("AUTORIZO")

            Select Case rdBuscar("COBRADO")
                Case -1
                    lblStatus.Text = "No ha sido autorizado"
                    btnAutorizar.Text = "Autorizar"
                    btnAutorizar.Text = True

                Case 0
                    lblStatus.Text = "Autorizado"
                    btnAutorizar.Text = ""
                    btnAutorizar.Text = False
                Case 1
                    lblStatus.Text = "Aplicado"
                    btnAutorizar.Text = ""
                    btnAutorizar.Text = False
                Case 2
                    lblStatus.Text = "Aplicado en nomina"
                    btnAutorizar.Text = ""
                    btnAutorizar.Text = False
            End Select
            

            ' dgProveedores.Rows.Clear()
            BLOQUEAR_ELEMENTOS()
        Else
            DESBLOQUEAR_ELEMENTOS()

            txtCodigo.Text = ""
            txtNombre.Text = ""
            txtMotivo.Text = ""
            txtMonto.Text = ""
            txtSolicita.Text = ""
            txtAutoriza.Text = ""

            lblStatus.Text = ""
            btnAutorizar.Text = ""
            btnAutorizar.Text = False

            txtCodigo.Focus()

            txtCodigo.Select()
            Application.DoEvents()
        End If

       

    End Sub
    Sub DESBLOQUEAR_ELEMENTOS()
        txtCodigo.Enabled = True
        txtNombre.Enabled = True
        txtMotivo.Enabled = True
        txtMonto.Enabled = True
        txtSolicita.Enabled = True
        txtAutoriza.Enabled = True



    End Sub
    Sub BLOQUEAR_ELEMENTOS()
        txtCodigo.Enabled = False
        txtNombre.Enabled = False
        txtMotivo.Enabled = False
        txtMonto.Enabled = False
        txtSolicita.Enabled = False
        txtAutoriza.Enabled = False



    End Sub

    Sub CargaEmpleados()

        txtCodigo.AutoCompleteCustomSource.Clear()
        txtSolicita.AutoCompleteCustomSource.Clear()


        Dim cnConn As New SqlConnection
        cnConn.ConnectionString = CitraConnection

        Dim strSql As String
        Dim cmdBuscar As New SqlCommand
        Dim rdBuscar As SqlDataReader

        'PENDIENTE  WHERE DEPTO IN (" & ClavesDepto & ")

        strSql = "SELECT E.CLAVE,NOMBRE_COMPLETO FROM EMPLEADOS E INNER JOIN DEPARTAMENTOS D ON E.DEPTO=D.CLAVE WHERE ES_PRODUCCION=1 AND D.ESTATUS=1 AND E.ESTATUS<>'B' ORDER BY NOMBRE_COMPLETO"

        cnConn.Open()
        cmdBuscar.Connection = cnConn
        cmdBuscar.CommandText = strSql
        Try
            rdBuscar = cmdBuscar.ExecuteReader

            Dim Clave As String = ""

            Do While rdBuscar.Read
                txtCodigo.AutoCompleteCustomSource.Add(CStr(rdBuscar("CLAVE").ToString.Trim))
                txtSolicita.AutoCompleteCustomSource.Add(rdBuscar("NOMBRE_COMPLETO"))
            Loop

        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Information, "Aviso")

        End Try




    End Sub

    Private Sub txtCodigo_KeyDown(sender As Object, e As System.Windows.Forms.KeyEventArgs) Handles txtCodigo.KeyDown
        If e.KeyCode = Keys.Enter Then
            Dim IDEMPLEADO As Integer = BuscarIntDatoCitra("SELECT IDEMPLEADO FROM EMPLEADOS WHERE CLAVE='" & txtCodigo.Text & "' AND ESTATUS<>'B'")
            If IDEMPLEADO > 0 Then
                txtNombre.Text = BuscarStrDatoCitra("SELECT NOMBRE_COMPLETO FROM EMPLEADOS WHERE IDEMPLEADO=" & IDEMPLEADO)
                txtAutoriza.Text = BuscarStrDatoCitra("SELECT NOMBRE_RESP FROM DEPARTAMENTOS D INNER JOIN EMPLEADOS E ON D.CLAVE=E.DEPTO WHERE E.IDEMPLEADO=" & IDEMPLEADO)
                txtMotivo.Focus()
            Else
                MsgBox("El empleado indicado no existe o esta inactivo", MsgBoxStyle.Exclamation, "Aviso")
                txtCodigo.Clear()
                txtCodigo.Focus()
            End If
        ElseIf e.KeyCode = Keys.Tab Then
            Dim IDEMPLEADO As Integer = BuscarIntDatoCitra("SELECT IDEMPLEADO FROM EMPLEADOS WHERE CLAVE='" & txtCodigo.Text & "' AND ESTATUS<>'B'")
            If IDEMPLEADO > 0 Then
                txtNombre.Text = BuscarStrDatoCitra("SELECT NOMBRE_COMPLETO FROM EMPLEADOS WHERE IDEMPLEADO=" & IDEMPLEADO)
                txtAutoriza.Text = BuscarStrDatoCitra("SELECT NOMBRE_RESP FROM DEPARTAMENTOS D INNER JOIN EMPLEADOS E ON D.CLAVE=E.DEPTO WHERE E.IDEMPLEADO=" & IDEMPLEADO)
                txtMotivo.Focus()
            Else
                MsgBox("El empleado indicado no existe o esta inactivo", MsgBoxStyle.Exclamation, "Aviso")
                txtCodigo.Clear()
                txtCodigo.Focus()

            End If
        ElseIf e.KeyCode = Keys.F3 Then
            Dim EMPLEADOS As New frmCat_Empleados
            EMPLEADOS.ShowDialog()
            If EMPLEADOS.DialogResult = Windows.Forms.DialogResult.OK Then
                Dim IDEMPLEADO As Integer = BuscarIntDatoCitra("SELECT IDEMPLEADO FROM EMPLEADOS WHERE CLAVE='" & txtCodigo.Text & "' AND ESTATUS<>'B'")
                txtNombre.Text = BuscarStrDatoCitra("SELECT NOMBRE_COMPLETO FROM EMPLEADOS WHERE IDEMPLEADO=" & IDEMPLEADO)
                txtAutoriza.Text = BuscarStrDatoCitra("SELECT NOMBRE_RESP FROM DEPARTAMENTOS D INNER JOIN EMPLEADOS E ON D.CLAVE=E.DEPTO WHERE E.IDEMPLEADO=" & IDEMPLEADO)
                txtMotivo.Focus()

            End If
        End If
    End Sub

    Private Sub txtCodigo_TextChanged(sender As System.Object, e As System.EventArgs) Handles txtCodigo.TextChanged

    End Sub

    Private Sub txtMotivo_KeyDown(sender As Object, e As System.Windows.Forms.KeyEventArgs) Handles txtMotivo.KeyDown
        If e.KeyCode = Keys.Enter Then
            txtMonto.Focus()

        ElseIf e.KeyCode = Keys.Tab Then
            txtMonto.Focus()

        End If
    End Sub

    Private Sub txtMotivo_TextChanged(sender As System.Object, e As System.EventArgs) Handles txtMotivo.TextChanged

    End Sub

    Private Sub txtMonto_KeyDown(sender As Object, e As System.Windows.Forms.KeyEventArgs) Handles txtMonto.KeyDown
        If e.KeyCode = Keys.Enter Then
            txtSolicita.Focus()
            If Len(txtMonto.Text) > 0 Then
                txtMonto.Text = FormatCurrency(txtMonto.Text)
            End If

        ElseIf e.KeyCode = Keys.Tab Then
            txtSolicita.Focus()
            If Len(txtMonto.Text) > 0 Then
                txtMonto.Text = FormatCurrency(txtMonto.Text)
            End If
        End If
    End Sub

    Private Sub txtMonto_TextChanged(sender As System.Object, e As System.EventArgs) Handles txtMonto.TextChanged

    End Sub

    Private Sub btnCancelar_Click(sender As System.Object, e As System.EventArgs) Handles btnCancelar.Click
        Me.DialogResult = Windows.Forms.DialogResult.Cancel
        Close()

    End Sub

    Private Sub btnGuardar_Click(sender As System.Object, e As System.EventArgs) Handles btnGuardar.Click
        If txtCodigo.Text = "" Then
            MsgBox("Debes indicar la clave del empleado", MsgBoxStyle.Exclamation, "Aviso")
            Exit Sub
        End If

        If txtMotivo.Text = "" Then
            MsgBox("Debes indicar el motivo de la consecuencia", MsgBoxStyle.Exclamation, "Aviso")
            Exit Sub

        End If

        If Len(txtMonto.Text) = 0 Then
            MsgBox("Debes indicar el monto de la consecuencia", MsgBoxStyle.Exclamation, "Aviso")
            Exit Sub
        End If

        If txtSolicita.Text = "" Then
            MsgBox("Debes indicar el nombre del solicitante", MsgBoxStyle.Exclamation, "Aviso")
            Exit Sub

        End If

        If txtAutoriza.Text = "" Then
            txtAutoriza.Text = " "
        End If

        Dim CONS As New clsConsecuencias
        CONS.Insertar(txtCodigo.Text, CDbl(txtMonto.Text), txtMotivo.Text, txtSolicita.Text, txtAutoriza.Text)
        Call SetSqlCitra("UPDATE ACUMULADOS SET CONSECUENCIAS=CONSECUENCIAS+" & txtMonto.Text & " WHERE IDACUMULADO=" & IDACUMULADO)
       
        Me.DialogResult = Windows.Forms.DialogResult.OK

        Close()

    End Sub

    Private Sub btnTodosRegresar_Click(sender As System.Object, e As System.EventArgs) Handles btnTodosRegresar.Click
        Me.DialogResult = Windows.Forms.DialogResult.Cancel
        Close()

    End Sub
End Class